﻿/**  版本信息模板在安装目录下，可自行修改。
* ProductType.cs
*
* 功 能： N/A
* 类 名： ProductType
*
* Ver    变更日期             负责人  变更内容
* ───────────────────────────────────
* V0.01  2013-4-15 13:34:44   N/A    初版
*
* Copyright (c) 2012 Maticsoft Corporation. All rights reserved.
*┌──────────────────────────────────┐
*│　此技术信息为本公司机密信息，未经本公司书面同意禁止向第三方披露．　│
*│　版权所有：动软卓越（北京）科技有限公司　　　　　　　　　　　　　　│
*└──────────────────────────────────┘
*/
using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;

namespace DMZS.DAL
{
    /// <summary>
    /// 数据访问类:ProductType
    /// </summary>
    public partial class ProductType
    {
        public ProductType()
        {
        }

        #region  BasicMethod

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(long ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from ProductType");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.BigInt)
			};
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public long Add(DMZS.Model.ProductType model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into ProductType(");
            strSql.Append("Code,Name,ParentID)");
            strSql.Append(" values (");
            strSql.Append("@Code,@Name,@ParentID)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.NVarChar,50),
					new SqlParameter("@Name", SqlDbType.NVarChar,100),
					new SqlParameter("@ParentID", SqlDbType.BigInt,8)};
            parameters[0].Value = model.Code;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.ParentID;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt64(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(DMZS.Model.ProductType model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update ProductType set ");
            strSql.Append("Code=@Code,");
            strSql.Append("Name=@Name,");
            strSql.Append("ParentID=@ParentID");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.NVarChar,50),
					new SqlParameter("@Name", SqlDbType.NVarChar,100),
					new SqlParameter("@ParentID", SqlDbType.BigInt,8),
					new SqlParameter("@ID", SqlDbType.BigInt,8)};
            parameters[0].Value = model.Code;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.ParentID;
            parameters[3].Value = model.ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(long ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ProductType ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.BigInt)
			};
            parameters[0].Value = ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ProductType ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.ProductType GetModel(long ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,Code,Name,ParentID from ProductType ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.BigInt)
			};
            parameters[0].Value = ID;

            DMZS.Model.ProductType model = new DMZS.Model.ProductType();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.ProductType GetModel(string Code)
        {
            var strSql = @"
select top 1 c.ID,c.Code,c.Name,isnull(c.ParentID,0) ParentID,
	p.Code ParentCode,p.Name ParentName
from ProductType c
left join ProductType p on p.ID=c.ParentID
where c.Code=@Code";
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.NVarChar, 50)
			};
            parameters[0].Value = Code;

            DMZS.Model.ProductType model = new DMZS.Model.ProductType();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.ProductType DataRowToModel(DataRow row)
        {
            DMZS.Model.ProductType model = new DMZS.Model.ProductType();
            if (row != null)
            {
                if (row["ID"] != null && row["ID"].ToString() != "")
                {
                    model.ID = long.Parse(row["ID"].ToString());
                }
                if (row["Code"] != null)
                {
                    model.Code = row["Code"].ToString();
                }
                if (row["Name"] != null)
                {
                    model.Name = row["Name"].ToString();
                }
                if (row["ParentID"] != null && row["ParentID"].ToString() != "")
                {
                    model.ParentID = long.Parse(row["ParentID"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,Code,Name,ParentID ");
            strSql.Append(" FROM ProductType ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,Code,Name,ParentID ");
            strSql.Append(" FROM ProductType ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM ProductType ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.ID desc");
            }
            strSql.Append(")AS Row, T.*  from ProductType T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "ProductType";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod


        /// <summary>
        /// 获得数据列表 扩展
        /// </summary>
        public DataSet GetListForUIExpand(string strWhere)
        {
            var strSql = @"
select c.ID,c.Code,c.Name,isnull(c.ParentID,0) ParentID,
	p.Code ParentCode,p.Name ParentName
from ProductType c
left join ProductType p on p.ID=c.ParentID";
            if (strWhere.Trim() != string.Empty)
            {
                strSql += " where " + strWhere;
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据 扩展
        /// 返回-1001:编号重复; 
        /// </summary>
        public long AddExpand(DMZS.Model.ProductType model, long ParentID)
        {
            var strSql = @"
declare @typeNoCount int
select @typeNoCount=count(1) from ProductType where Code=@Code
if @typeNoCount>0 begin select -1001 return end 

insert into ProductType(Code,Name,ParentID) values (@Code,@Name,@ParentID)
select @@IDENTITY";

            var parameters = new SqlParameter[] { 
                    new SqlParameter("@Code", SqlDbType.NVarChar, 50),
                    new SqlParameter("@Name", SqlDbType.NVarChar, 100),
                    new SqlParameter("@ParentID", SqlDbType.BigInt) };

            parameters[0].Value = model.Code;
            parameters[1].Value = model.Name;
            parameters[2].Value = ParentID;

            var obj = DbHelperSQL.GetSingle(strSql, parameters);
            if (obj == null) return 0;
            else return Convert.ToInt64(obj);
        }

        /// <summary>
        /// 获取数据行
        /// </summary>
        /// <param name="ParentTypeID"></param>
        /// <returns></returns>
        public DataTable GetRecordByID(long ProductTypeID)
        {
            var strSql = @"
select c.ID,c.Code,c.Name,c.ParentID,
	p.Code ParentCode,p.Name ParentName
from ProductType c
left join ProductType p on p.ID=c.ParentID
where c.ID=@ID";
            var parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.BigInt) };
            parameters[0].Value = ProductTypeID;

            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            return ds.Tables[0];
        }
        /// <summary>
        /// 删除一条数据,及其子级
        /// -1:删除失败，-2：其下有产品
        /// </summary>
        public int DeleteExpand(long ID)
        {
            var parameters = new SqlParameter[] { 
                new SqlParameter("@ID", SqlDbType.BigInt),
                new SqlParameter("@CanDelete", SqlDbType.Bit),
                new SqlParameter("@Success", SqlDbType.Bit)
            };
            parameters[0].Value = ID;
            parameters[1].Direction = ParameterDirection.Output;
            parameters[2].Direction = ParameterDirection.Output;


            DbHelperSQL.RunProcedure("pDeleteProdutType", parameters);

            if (Convert.ToBoolean(parameters[0].Value) && Convert.ToBoolean(parameters[1].Value))
            {
                return 1;
            }
            else if (!Convert.ToBoolean(parameters[1].Value))
            {
                return -2;
            }
            else if (!Convert.ToBoolean(parameters[2].Value))
            {
                return -1;
            }
            return 1;
        }

        /// <summary>
        /// 获取第一级子项个数
        /// </summary>
        /// <param name="ProductTypeID"></param>
        /// <returns></returns>
        public int GetChildrenCount(long ProductTypeID)
        {
            var sqlStr = "select count(1) from ProductType where ParentID=" + ProductTypeID.ToString();
            var result = DbHelperSQL.GetSingle(sqlStr);

            if (result == null)  return -1;
            else return Convert.ToInt32(result);
        }

        /// <summary>
        /// 获取数据行
        /// </summary>
        /// <param name="Code"></param>
        /// <returns></returns>
        public DataRow GetRecordByNo(string Code)
        {
            var strSql = @"
select top 1 c.ID,c.Code,c.Name,c.ParentID,
	p.Code ParentCode,p.Name ParentName
from ProductType c
left join ProductType p on p.ID=c.ParentID
where Code=@Code";
            var parameters = new SqlParameter[] { new SqlParameter("@Code", SqlDbType.NVarChar, 50) };
            parameters[0].Value = Code;

            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return ds.Tables[0].Rows[0];
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取数据 对话框
        /// </summary>
        /// <param name="Code"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
        public DataSet GetListForDialog(string Code, string Name)
        {
            var strSql = @"
select 
    c.ID,c.Code,c.Name,c.ParentID,
	p.Code ParentCode,p.Name ParentName
from ProductType c
left join ProductType p on p.ID=c.ParentID
where c.Code like '%'+@Code+'%' and c.Name like '%'+@Name+'%' ";

            var parameters = new SqlParameter[] { 
                new SqlParameter("@Code", SqlDbType.NVarChar, 50),
                new SqlParameter("@Name", SqlDbType.NVarChar, 100)};
            parameters[0].Value = Code;
            parameters[1].Value = Name;

            return DbHelperSQL.Query(strSql.ToString(), parameters);
        }
    }
}

